package com.hkts.reportform.purchasepound.service.impl;

import com.hkts.reportform.purchasepound.dao.PurchasePoundDao;
import com.hkts.reportform.purchasepound.service.PurchasePoundService;
import com.hkts.reportform.utils.CustomPage;
import com.hkts.reportform.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNamePurchasePoundServiceImpl
 * @Description
 * @Author yck
 * @Date2020-11-27 9:37
 * @Version V1.0
 **/
@Service("purchasePoundServiceImpl")
public class PurchasePoundServiceImpl implements PurchasePoundService {
    @Autowired
    private PurchasePoundDao purchasePoundDaoImp;

    @Override
    public Map purchasePoundCurrentDay() {
        Map map=new HashMap();
        //获取本日磅单数目
        Integer poundCountByDay = purchasePoundDaoImp.getPoundCountByDay(DateUtil.getTomorrow(0), DateUtil.getTomorrow());
        DecimalFormat decimalFormat=new DecimalFormat("0.00");
        map.put("count",poundCountByDay);
        //获取当日净重综合
        Map<String, Object> poundWeightAndRate = purchasePoundDaoImp.getPoundWeightAndRate(DateUtil.getTomorrow(0), DateUtil.getTomorrow(1));
        map.put("netWeight",poundWeightAndRate.get("fSuttle"));
        if(Float.parseFloat(poundWeightAndRate.get("fdue").toString())>0){
          Float rate= Float.parseFloat(poundWeightAndRate.get("fSuttle").toString())/Float.parseFloat(poundWeightAndRate.get("fdue").toString());
            map.put("rate",decimalFormat.format(rate));
        }else{
            map.put("rate","0.00");
        }
        return map;
    }

    @Override
    public Map purchasePoundTrend(String dateType, String beginDate, String endDate) throws ParseException {
        Map<String,Object> data=new HashMap<String,Object>();
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
       List<Map<String,Object>> list= purchasePoundDaoImp.purchasePoundTrend(dateArray.get(0),dateArray.get(dateArray.size()-1));
       List<Integer> valueList=new ArrayList<>();
        List<Integer> resultList=new ArrayList<>();
       List<String> lists=new ArrayList<String>();
        list.stream().forEach(map->{
            valueList.add(Integer.parseInt(map.get("count").toString()));
            lists.add(map.get("dFirstDate").toString());
        });
        for(int i=0;i<dateArray.size();i++){
            if(lists.contains(dateArray.get(i))){
                resultList.add(valueList.get(i));
            }else{
                resultList.add(0);
            }
        }
        data.put("dateArray",DateUtil.dateListConvert(dateArray));
        data.put("series",resultList);
        return data;
    }

    @Override
    public CustomPage purchasePoundPage(String dateType, String beginDate, String endDate, CustomPage page) {
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
        DecimalFormat decimalFormat=new DecimalFormat("0.00");
        Map<String, Object> purchaseCountByDate = purchasePoundDaoImp.getPurchaseCountByDate(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        page.setTotalRows(Integer.parseInt(purchaseCountByDate.get("count").toString()));
        int firstRow = page.getPageSize() * (page.getPageNumber() - 1) + 1;
        int lastRow = page.getPageSize() * page.getPageNumber();
        List<Map<String, Object>> purchasePage = purchasePoundDaoImp.getPurchasePage(dateArray.get(0), dateArray.get(dateArray.size() - 1), firstRow, lastRow);
        purchasePage.stream().forEach(map->{
            if(map.get("fSuttle")==null){
                map.put("fSuttle","/");
                map.put("rate","/");
            }else{
                Float.parseFloat(map.get("fSuttle").toString());
                String format = decimalFormat.format((Float.parseFloat(map.get("fdue").toString()) -Float.parseFloat(map.get("fSuttle").toString()))/Float.parseFloat(map.get("fdue").toString())*100);
                map.put("rate",format);
            }
        });
        page.setResultData(purchasePage);
        return page;
    }
}
